//
//  HomeViewController.m
//  LazyCat
//
//  Created by zhanshu on 15/9/7.
//  Copyright (c) 2015年 zhanshu. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeHeaderView.h"//滑动广告部分
#import "HomeSectionHeaderTwo.h"//
#import "ProductCollectionViewCell.h"//大图
#import "NearCollectionViewCell2.h"//列表
#import "TypeCollectionViewCell.h"//选择类型
#import "SearchViewController.h"//搜索页面
#import "HomeSectionFooterView.h"//底部
#import "SelectNearProprieterController.h"
#import "Html5ViewController.h"
@interface HomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,ProductCountDelegate,SeletTypeDelegate,UIAlertViewDelegate,FFScrollViewDelegate>
{
    UIButton *logoBtn;//logo
    UIButton *titleBtn;//选择社长
    UIImageView *arrowImg;
    NSMutableArray *buyCountArr;//购买的数量
    
    NSMutableArray *adsArr;//轮播图
    NSDictionary *modelDic;//四个小分类
    NSMutableArray *shopadsArr;//
    NSMutableArray *productsArr;//店铺商品
    NSString *updateURL;//版本更新地址
    
    IBOutlet UIView *noNetWorkView;
}
@property (weak, nonatomic) IBOutlet UICollectionView *colloctionView;
@property (strong, nonatomic) HomeHeaderView *homeHeaderView;//collection header
@property (strong, nonatomic) HomeSectionHeaderTwo *homeHeaderViewTwo;
@property (strong, nonatomic) HomeSectionFooterView *footer;
@end

@implementation HomeViewController
- (void)viewDidAppear:(BOOL)animated {
    
    if (![[AppData share].proprieter isEqualToString:titleBtn.currentTitle]) {
        [self getMess];
    }
    
    CGFloat staticWidth = ScreenWidth-(logoBtn.right+8)*2;
    CGFloat width = [Utils widthForString:[AppData share].proprieter fontSize:15 andHeight:25];
    width = (width+10)>staticWidth?staticWidth:width;
    [titleBtn setTitle:[AppData share].proprieter forState:UIControlStateNormal];
    titleBtn.frame = CGRectMake(logoBtn.right+(staticWidth-width)/2+5, 20+8, width, 25);
    arrowImg.frame = CGRectMake(titleBtn.right, 20+(44-6)/2, 11, 6);
}
- (void)clearBuyCount {
    for (int i = 0; i < buyCountArr.count; i ++) {
        [buyCountArr replaceObjectAtIndex:i withObject:@"0"];
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = BackColor;
    [AppData share].doFirst = NO;
    [NSUserDefualManager storageValue:[NSNumber numberWithBool:YES] forKey:@"DoFirstLogin"];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearBuyCount) name:@"ClearBuyCount" object:nil];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [_colloctionView registerNib:[UINib nibWithNibName:@"ProductCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cell"];
    [_colloctionView registerNib:[UINib nibWithNibName:@"NearCollectionViewCell2" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cell2"];
    [_colloctionView registerNib:[UINib nibWithNibName:@"TypeCollectionViewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"cell1"];
    
    [_colloctionView registerClass:NSClassFromString(@"UICollectionReusableView")  forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header"];
    [_colloctionView registerClass:NSClassFromString(@"UICollectionReusableView")  forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header1"];
    [_colloctionView registerClass:NSClassFromString(@"UICollectionReusableView")  forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"Header3"];
    [_colloctionView registerClass:NSClassFromString(@"UICollectionReusableView")  forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer"];
    [_colloctionView registerClass:NSClassFromString(@"UICollectionReusableView")  forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"Footer2"];
    _colloctionView.backgroundColor = BackColor;
    _colloctionView.alwaysBounceVertical = YES;
    
    //记录当前页面购买的数量
    buyCountArr = [[NSMutableArray alloc] init];
    
    adsArr = [[NSMutableArray alloc] init];//轮播图
    modelDic = [[NSDictionary alloc] init];//小分类
    shopadsArr = [[NSMutableArray alloc] init];//店铺广告
    productsArr = [[NSMutableArray alloc] init];//商品
    
    
    [self initTopView];
    [self addRefresh];
    [self getMessFromLoca];//从本地获取数据
    
//    [self getMess];
    //先进行登陆 
    [self loginFirst];
    [self  performSelector:@selector(getVersionMess) withObject:nil afterDelay:2];
}
- (void)loginFirst {
    NSString *name = [NSUserDefualManager getValueByKey:UserName];
    NSString *psw = [NSUserDefualManager getValueByKey:Psw];
//    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NTNetUtil reLoginNum:name psw:psw complate:^(BOOL success) {
//        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self getMess];
    }];
}
- (void)initTopView {
    [self initNav:@"" doBack:NO];
    
    logoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    logoBtn.frame = CGRectMake(5, 20+(44-42)/2, 42, 37);
    logoBtn.adjustsImageWhenHighlighted = NO;
    [logoBtn setBackgroundImage:[UIImage imageNamed:@"首页_r1_c3.png"] forState:UIControlStateNormal];
    [self.navBar addSubview:logoBtn];
    
    CGFloat staticWidth = ScreenWidth-(logoBtn.right+8)*2;
    CGFloat width = [Utils widthForString:[AppData share].proprieter fontSize:15 andHeight:25];
    width = (width+10)>staticWidth?staticWidth:width;
    titleBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    titleBtn.frame = CGRectMake(logoBtn.right+(staticWidth-width)/2+5, 20+8, width, 25);
    [titleBtn setTitle:[AppData share].proprieter forState:UIControlStateNormal];
    [titleBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    titleBtn.titleLabel.font = FontSet(15);
    [titleBtn addTarget:self action:@selector(selectProprieter) forControlEvents:UIControlEventTouchUpInside];
    [self.navBar addSubview:titleBtn];
    
    arrowImg = [[UIImageView alloc] initWithFrame:CGRectMake(titleBtn.right, 20+(44-6)/2, 11, 6)];
    arrowImg.image = [UIImage imageNamed:@"首页_r3_c38.png"];
    [self.navBar addSubview:arrowImg];
    
    //搜索
    UIButton *signBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    signBtn.frame = CGRectMake(ScreenWidth-20-8, 20+(44-20)/2, 20, 20);
    [signBtn setHitTestEdgeInsets:UIEdgeInsetsMake(-10, -10, -10, -10)];
    [signBtn setImage:[UIImage imageNamed:@"首页_r6_c25.png"] forState:UIControlStateNormal];
    [signBtn addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    [self.navBar addSubview:signBtn];
}
- (void)addRefresh {
    _colloctionView.header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getMess];
    }];
}
#pragma mark --collection  datasource  delegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return shopadsArr.count;
    }else {
        return productsArr.count;
    }
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 3;
}
//定义每个UICollectionView 的大小
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2) {
        CGFloat itemWidth = (ScreenWidth - 30)/2;
        CGFloat itemHeight = 210*itemWidth/145;
        return CGSizeMake(itemWidth, itemHeight);
    }else if (indexPath.section == 1) {
        
        CGFloat itemHeight = 125*ScreenWidth/320;
        return CGSizeMake(ScreenWidth, itemHeight);
    }
    else {
        return CGSizeMake(ScreenWidth, 70);
    }
    
}
//定义每个Section 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (section == 2) {
        return UIEdgeInsetsMake(0, 10, 10, 10);//分别为上、左、下、右
    }else {
        return UIEdgeInsetsMake(10, 0, 0, 0);//分别为上、左、下、右
    }
    
}
//每个UICollectionView展示的内容
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        static NSString *Cell = @"cell1";
        TypeCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:Cell forIndexPath:indexPath];
        cell.delegate = self;
        return cell;
    }else if (indexPath.section == 1) {
        static NSString *Cell = @"cell2";
        NearCollectionViewCell2 * cell = [collectionView dequeueReusableCellWithReuseIdentifier:Cell forIndexPath:indexPath];
        if (shopadsArr.count>0) {
            NSDictionary *dic = [shopadsArr objectAtIndex:indexPath.row];
            [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[dic[@"img"] safeString]] placeholderImage:[UIImage imageNamed:@"nr-no-pic.png"]];
        }
        return cell;
    }
    else {
        static NSString *Cell = @"cell";
        ProductCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:Cell forIndexPath:indexPath];
        cell.delegate = self;
        if (productsArr.count>0) {
            NSDictionary *dic = productsArr[indexPath.row];
            cell.productImg.image = [UIImage imageNamed:@"product-no-pic.png"];
            [cell.productImg sd_setImageWithURL:[NSURL URLWithString:[dic[@"img"] safeString]] placeholderImage:[UIImage imageNamed:@"product-no-pic.png"]];
            cell.productName.text = dic[@"name"];
            cell.productDetailLable.text = dic[@"roperty"];
            cell.nowPriceLable.text = [NSString stringWithFormat:@"¥%@",dic[@"sale"]];//现价
            cell.oldPriceLable.text = [NSString stringWithFormat:@"¥%@",dic[@"market"]];//原价
            //改变空间大小
            [cell theNowPrice:dic[@"sale"] oldPrice:dic[@"market"]];
            
            NSInteger count = [buyCountArr[indexPath.row] intValue];
            if (count == 0) {
                cell.reduceBtn.hidden = YES;
                cell.countLable.hidden = YES;
            }else {
                cell.reduceBtn.hidden = NO;
                cell.countLable.hidden = NO;
            }
            cell.countLable.text = buyCountArr[indexPath.row];
        }
        
        return cell;
    }
    
}
- (UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        NSString *reuseIdentifier;
        if ([kind isEqualToString: UICollectionElementKindSectionHeader ]){
            reuseIdentifier = @"Header";
        }else {
            reuseIdentifier = @"Footer2";
            UICollectionReusableView *view =  [collectionView dequeueReusableSupplementaryViewOfKind :kind   withReuseIdentifier:reuseIdentifier   forIndexPath:indexPath];
            return view;
        }
        UICollectionReusableView *view =  [collectionView dequeueReusableSupplementaryViewOfKind :kind   withReuseIdentifier:reuseIdentifier   forIndexPath:indexPath];
        if (!_homeHeaderView) {
            _homeHeaderView = [[[NSBundle mainBundle] loadNibNamed:@"HomeHeaderView" owner:self options:nil] firstObject];
            _homeHeaderView.frame = CGRectMake(0, 0, ScreenWidth, ScreenWidth*130/320);
            _homeHeaderView.picScrollView.pageViewDelegate = self;
            [view addSubview:_homeHeaderView];
        }
        return view;
    }else if (indexPath.section == 1) {
        NSString *reuseIdentifier;
        if ([kind isEqualToString: UICollectionElementKindSectionHeader ]){
            reuseIdentifier = @"Header3";
        }else {
            reuseIdentifier = @"Footer2";
        }
        UICollectionReusableView *view =  [collectionView dequeueReusableSupplementaryViewOfKind :kind   withReuseIdentifier:reuseIdentifier   forIndexPath:indexPath];
        return view;
    }
    else {
        
        if ([kind isEqualToString: UICollectionElementKindSectionFooter ]) {
            NSString *reuseIdentifier;
            reuseIdentifier = @"Footer";
            UICollectionReusableView *view =  [collectionView dequeueReusableSupplementaryViewOfKind :kind   withReuseIdentifier:reuseIdentifier   forIndexPath:indexPath];
            if (productsArr.count == 0) {
                _footer.hidden = YES;
            }else {
                _footer.hidden = NO;
            }
            if (!_footer) {
                _footer = [[[NSBundle mainBundle] loadNibNamed:@"HomeSectionFooterView" owner:self options:nil] firstObject];
                _footer.frame = CGRectMake(0, 0, ScreenWidth, 40);
                [_footer.lookMoreBtn addTarget:self action:@selector(lookMoreAction) forControlEvents:UIControlEventTouchUpInside];
                [view addSubview:_footer];
            }
            
            return view;
        }else {
            NSString *reuseIdentifier;
            reuseIdentifier = @"Header1";
            UICollectionReusableView *view =  [collectionView dequeueReusableSupplementaryViewOfKind :kind   withReuseIdentifier:reuseIdentifier   forIndexPath:indexPath];
            
            if (!_homeHeaderViewTwo) {
                _homeHeaderViewTwo = [[[NSBundle mainBundle] loadNibNamed:@"HomeSectionHeaderTwo" owner:self options:nil] firstObject];
                _homeHeaderViewTwo.frame = CGRectMake(0, 0, ScreenWidth, 30);
                _homeHeaderViewTwo.moreBtn.hidden = YES;
                _homeHeaderViewTwo.titleLable.text = @"社长热卖";
                [view addSubview:_homeHeaderViewTwo];
            }
            if (productsArr.count == 0) {
                _homeHeaderViewTwo.hidden = YES;
            }else {
                _homeHeaderViewTwo.hidden = NO;
            }
            
            return view;
        }
    }
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (section == 0 ) {
        return CGSizeMake(ScreenWidth, ScreenWidth*130/320);
    }else if (section == 1) {
        return CGSizeMake(ScreenWidth, .5);
    }
    else {
        if (productsArr.count == 0) {
            return CGSizeMake(ScreenWidth, .05);
        }else{
            return CGSizeMake(ScreenWidth, 30);
        }
    }
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    if (section == 0 ) {
        return CGSizeMake(ScreenWidth, .05);
    }else if (section == 1) {
        return CGSizeMake(ScreenWidth, .5);
    }
    else {
        if (productsArr.count == 0) {
            return CGSizeMake(ScreenWidth, .05);
        }else{
            return CGSizeMake(ScreenWidth, 40);
        }
    }
}
//UICollectionView被选中时调用的方法
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{

    
    NSString *requestUrl = @"";
    NSString *productImg = @"";
    NSString *titleName = @"";
    if (indexPath.section == 1) {
        NSDictionary *dic = [shopadsArr objectAtIndex:indexPath.row];
        
        // 推荐商品
        NSString *tempUrl = [NSString stringWithFormat:@"%@?appname=lanmao",dic[@"url"]];
//        requestUrl = dic[@"url"];
        requestUrl = tempUrl;
    
        titleName = dic[@"caption"];
    }else if (indexPath.section == 2) {
        NSDictionary *dic = [productsArr objectAtIndex:indexPath.row];
        requestUrl = dic[@"url"];
        productImg = dic[@"img"];
        titleName = dic[@"name"];
    }

    //商品的详情页 显示分享按钮 不显示刷新按钮
    [self pushToDetailViewTitle:titleName doDetailType:YES doComeFromeHomeView:indexPath.section == 2?NO:YES requestUrl:requestUrl productImg:productImg];
}
#pragma mark - 点击事件
- (void)searchAction {
    SearchViewController *searchVC = [[SearchViewController alloc] init];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:searchVC];
    nav.navigationBarHidden = YES;
    [self presentViewController:nav animated:YES completion:^{
        
    }];
}
- (void)selectProprieter {
    //选择社长
    
    SelectNearProprieterController *selectNear = [[SelectNearProprieterController alloc] init];
    [self.navigationController pushViewController:selectNear animated:YES];
    
}
- (void)lookMoreAction {
    //查看更多热卖信息
    [[AppData share].baseTabVC changeBottomBtnWithIndex:1];
}
- (void)theSelectTypeIs:(NSInteger)tag {
    if (tag == 3) {
        //懒猫超市
        [[AppData share].baseTabVC changeBottomBtnWithIndex:1];
    }else {
        NSString *url = @"";
        NSString *titleStr = @"";
        if (tag == 0) {
            url = modelDic[@"hongbao"];
            titleStr = @"抢红包";
        }else if (tag == 1) {
            url = modelDic[@"jifen"];
            titleStr = @"积分兑换";
        }else if (tag == 2) {
            url = modelDic[@"cuxiao"];
            titleStr = @"促销";
        }
        
        [self pushToDetailViewTitle:titleStr doDetailType:NO doComeFromeHomeView:YES requestUrl:url productImg:@""];
    }
}
#pragma mark- 获取数据
//获取本地缓存
- (void)getMessFromLoca {
    NSArray *arr = [NSUserDefualManager getValueByKey:LC_StorageHomeMess];
    if (arr) {
        for (int i = 0 ; i < arr.count; i ++) {
            if (i == 0) {
                [adsArr addObjectsFromArray:arr[0]];
                [_homeHeaderView.picScrollView initWithImgs:adsArr];
            }else if (i == 1) {
                modelDic = arr[1];
            }else if (i == 2) {
                [shopadsArr addObjectsFromArray:arr[2]];
            }else if (i == 3) {
                [productsArr addObjectsFromArray:arr[3]];
                [self theShopCount];
            }
        }
        [_colloctionView reloadData];
    }
    
}
#pragma mark - //请求数据
- (void)getMess {
//    ShopCommonModel *model = [ShopCommonModel getShopMess];
//    NSString *url = [NSString stringWithFormat:GetHomeMessUrl,model.shopuser,[model.x doubleValue],[model.y doubleValue],[[AppData share].username safeString],[Utils md5SignStr]];
    NSString *url = @"";
    url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    [NTNetUtil sendRequestWithUrl:url requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
        NSDictionary *dic = networkRequest.responseData;
        NSString * r = dic[@"r"];
        if ([r isEqualToString:@"T"]) {
            [adsArr removeAllObjects];
            modelDic = @{};
            [shopadsArr removeAllObjects];
            [productsArr removeAllObjects];
            
            //轮播图
            [adsArr addObjectsFromArray:dic[@"ads"]];
            //model
            modelDic = dic[@"model"];
            //商店广告
            [shopadsArr addObjectsFromArray:dic[@"shopads"]];
            //商品
            [productsArr addObjectsFromArray:dic[@"products"]];
            
            [self theShopCount];
            //购物车显示数量
            [[AppData share].baseTabVC theShoppingCartCount:dic[@"shopcart"]];
            //缓存数据
            [NSUserDefualManager storageValue:@[adsArr,modelDic,shopadsArr,productsArr] forKey:@"LC_StorageHomeMess"];
            
            noNetWorkView.hidden = YES;
            _colloctionView.hidden = NO;
            
        }else {
            [[MessShowView share] showWithMess:dic[@"m"]];
            
            //当没有网络 没有本地缓存的时候隐藏
            if (kNetworkNotReachability) {
                NSArray *arr = [NSUserDefualManager getValueByKey:LC_StorageHomeMess];
                if (!arr) {
                    noNetWorkView.hidden = NO;
                    _colloctionView.hidden = YES;
                }
            }
        }
        [_homeHeaderView.picScrollView initWithImgs:adsArr];
        [_colloctionView reloadData];
        [_colloctionView.header endRefreshing];
    }];
}
- (void)theShopCount {
    //根据返回的商品数量 计算购买个数
    [buyCountArr removeAllObjects];
    for (int i = 0; i < productsArr.count; i ++) {
        [buyCountArr addObject:@"0"];
    }
}
#pragma mark - 版本信息
- (void)getVersionMess {
    NSString *url = [NSString stringWithFormat:UpdateVersion,[Utils md5SignStr],@"1.0.1"];
    [NTNetUtil sendRequestWithUrl:url requestName:UpdateVersion method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
        NSDictionary *dic = networkRequest.responseData;
        NSString *suc = dic[@"r"];
        if ([suc isEqualToString:@"T"]) {
            NSDictionary *ios = dic[@"iOS"];
//            NSString *currentVS =  [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
            
            NSString *need = [ios[@"need"] safeString];
            NSString *note = [ios[@"note"] safeString];
            NSString *title = [ios[@"title"] safeString];
            NSString *isnew = [ios[@"isnew"] safeString];
            if ([isnew isEqualToString:@"false"]) {
                return ;
            }
            if ([need isEqualToString:@"false"]) {
                //不强制
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:note delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"更新", nil];
                alert.tag = 100;
                [alert show];
            }else {
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:note delegate:self cancelButtonTitle:@"更新" otherButtonTitles:nil, nil];
                alert.tag = 101;
                [alert show];
            }
            updateURL = [ios[@"url"] safeString];
        }
    }];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    
    if (alertView.tag == 100) {
        if (buttonIndex == 1) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",updateURL]]];
        }
    }else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://%@",updateURL]]];
    }
}
#pragma mark - 选择数量 delegate
- (void)collectionCell:(ProductCollectionViewCell *)cell btn:(UIButton *)btn countLable:(UILabel *)countLable{
    
    if (![AppData share].doLogin) {
        [[MessShowView share] showWithMess:@"请先登录"];
        [self goLoginView];
        return;
    }
    NSIndexPath *indexPath = [_colloctionView indexPathForCell:cell];
    NSInteger tag = btn.tag;
    
    NSDictionary *productDic = productsArr[indexPath.row];
//    NSString *string = [NSString stringWithFormat:ModifyShoppingCartCount,[AppData share].username,[ShopCommonModel getShopMess].shopuser,productDic[@"id"],tag==100?0:1,[Utils md5SignStr]];
    NSString *string = @"";
    [NTNetUtil sendRequestWithUrl:string requestName:nil method:NTRequestMethodGET parameters:nil block:^(NTHttpRequest *networkRequest, NSError *error) {
        NSDictionary *dic = networkRequest.responseData;
        NSString *suc = dic[@"r"];
        if ([suc isEqualToString:@"T"]) {
            //成功后改变商品数量
            [self changeProductCount:tag backCount:dic[@"totalcount"] indexPath:indexPath locCount:buyCountArr[indexPath.row]];
        }else {
//            [[MessShowView share] showWithMess:dic[@"m"]];
            [[MessShowView share] showWithMess:@"补货中"];
        }
    }];
    
}
- (void)changeProductCount:(NSInteger)tag backCount:(NSString *)count indexPath:(NSIndexPath*)indexPath locCount:(NSString *)locCount{
    if (tag == 100) {
        if (count == 0) {
            [buyCountArr replaceObjectAtIndex:indexPath.row withObject:locCount];
        }else {
            int ct = [locCount intValue]-1;
            [buyCountArr replaceObjectAtIndex:indexPath.row withObject:[NSString stringWithFormat:@"%d",ct]];
        }
        
        [[AppData share].baseTabVC doReduce:YES];
    }else {
        int ct = [locCount intValue]+1;
        [buyCountArr replaceObjectAtIndex:indexPath.row withObject:[NSString stringWithFormat:@"%d",ct]];
    }
    
    [[AppData share].baseTabVC theShoppingCartCount:count];
    
    [_colloctionView reloadData];
}
#pragma mark- 点击广告 展示详情
- (void)scrollViewDidClickedAtPage:(NSInteger)pageNumber {
    NSDictionary *selectDic = adsArr[pageNumber];
    if ([selectDic[@"url"] length] != 0) {
        [self pushToDetailViewTitle:selectDic[@"title"] doDetailType:NO doComeFromeHomeView:YES requestUrl:selectDic[@"url"] productImg:@""];
        
        
    }
}
#pragma mark - 跳转详情页面
- (void)pushToDetailViewTitle:(NSString *)title doDetailType:(BOOL)doDetailType doComeFromeHomeView:(BOOL)doComeFromeHomeView requestUrl:(NSString *)url productImg:(NSString *)productImg{
    Html5ViewController *html5VC = [[Html5ViewController alloc] init];
    html5VC.doComeFromeHomeView = doComeFromeHomeView;
    html5VC.doDetailType = doDetailType;
    html5VC.titleStr = title;
    html5VC.requestUrl = url;
    html5VC.shareImgUrl = productImg;
    [self.navigationController pushViewController:html5VC animated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
